package com.dhh.sky.widget;

import java.util.ArrayList;
import java.util.List;
import com.dhh.sky.R;
import com.dhh.sky.model.SkyDriveObject;
import com.dhh.sky.util.FileManager;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FolderChooserAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private Context context;
	private List<SkyDriveObject> files;  
	private FileManager fm;
	private String path;
	
	public FolderChooserAdapter(Context context){
		this.context = context;
		fm = new FileManager(context);
		inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		files = new ArrayList<SkyDriveObject>();  
	}
	
	public void loadFiles(){
		this.files.clear();
		for(SkyDriveObject file : fm.getFiles(path)){
			if(file.isFolder()){
				this.files.add(file);
			}
		}
	}

	@Override
	public int getCount() {
		return files.size();
	}

	@Override
	public SkyDriveObject getItem(int position) {
		return files.get(position);
	}

	@Override
	public long getItemId(int position) {
		long id = -1;
		if(position>files.size()-1){
			id = Long.valueOf(files.get(position).getId());
		}
		return id;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View view = inflater.inflate(R.layout.folder_choose_list_item,parent,false);
		final SkyDriveObject file = this.getItem(position);
		ImageView icon = (ImageView) view.findViewById(R.id.cli_icon);
		TextView name = (TextView) view.findViewById(R.id.cli_name);		
		
		icon.setImageResource(file.getIcon());
		name.setText(file.getName());
		
		return view;
	}
	
	public void setPath(String path) {
		this.path = path;
	}
}
